import { ref, toRefs } from 'vue'
import { useDictStore } from '@/store/login/dict'
import { selectTypeToData } from '@/service/system/dict'

export function useDict(...args: string[]) {
	const res = ref<{ [key: string]: any }>({})
	const dictStore = useDictStore()

	args.forEach(async (type: string) => {
		res.value[type] = []
		const dict = dictStore.dict[type]
		if (dict) {
			res.value[type] = dict
		} else {
			const dicts = await selectTypeToData(type)
			res.value[type] = dicts.map((item: any) => ({
				value: item.dictValue,
				label: item.dictLabel,
				tagType: item.tagType
			}))
			dictStore.dict[type] = res.value[type]
		}
	})
	return toRefs(res.value)
}
